/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.service.impl;

import com.lehe.common.Contants;
import com.lehe.common.MessageEnumDto;
import com.lehe.common.crypt.Md5Encrypt;
import com.lehe.common.exceptions.LHException;
import com.lehe.common.utils.StringUtil;
import com.lehe.dao.AgentInfoDao;
import com.lehe.dao.UserChargeGoldCoinLogDao;
import com.lehe.dao.UserManagerDao;
import com.lehe.entry.AgentInfoDto;
import com.lehe.entry.UserChargeGoldCoinLogDto;
import com.lehe.entry.UserInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 用户代理操作
 *
 * @author 王丹
 * @since 2017-02-26
 */
@Transactional
@Service
public class AgentInfoService extends AbstractService {

    @Autowired
    private AgentInfoDao agentInfoDao;
    
    @Autowired
    private UserManagerDao userManagerDao;
    
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserChargeGoldCoinLogDao userChargeGoldCoinLogDao;

    /**
     * 新增一个代理
     * @param userId 父代理用户ID
     * @param agentSubInfoDto 子代理用户ID,手机号码,登录后台密码
     * @return
     */
    public MessageEnumDto createAgent(Long userId, AgentInfoDto agentSubInfoDto){
        MessageEnumDto messageDto = MessageEnumDto.FAIL;
        UserInfoDto userInfoDto = userInfoService.queryUserInfo(userId, null);
        if(!StringUtil.isCreateAgent(userInfoDto.getUserLevel())){
            messageDto.setMessageValue("您的代理级别不够");
            return messageDto;
        }
        UserInfoDto userSubInfoDto = userInfoService.queryUserInfo(agentSubInfoDto.getUserId(), null);
        if(null == userSubInfoDto){
            messageDto.setMessageValue("您授权的用户不存在");
            return messageDto;
        }
        if(null!=agentInfoDao.queryAgentInfoByUserId(agentSubInfoDto.getUserId())){
            messageDto.setMessageValue("用户已经是代理,不需要重新授权！");
            return messageDto;
        }
        AgentInfoDto agentInfoDto = agentInfoDao.queryAgentInfoByUserId(userId);
        
        if(agentInfoDto.getAgentId() == null || agentInfoDto.getAgentId().intValue() <= 0){
        	messageDto.setMessageValue("您还不是代理，授权代理失败！");
            return messageDto;
        }
        
        if(!agentInfoDto.getAgentId().equals(userSubInfoDto.getFatherAgentId())){
            messageDto.setMessageValue("要授权的用户不是您的会员，授权代理失败！");
            return messageDto;
        }
        
        userSubInfoDto.setAgentId(agentSubInfoDto.getUserId());
        userSubInfoDto.setFatherAgentId(agentInfoDto.getAgentId());
        userSubInfoDto.setUserLevel(userInfoDto.getUserLevel() + 1);
        userInfoService.updateAccountByUserId(userSubInfoDto);

        agentSubInfoDto.setAgentId(agentSubInfoDto.getUserId());
        agentSubInfoDto.setfAgentId(agentInfoDto.getAgentId());
        agentSubInfoDto.setAgentPassword(Md5Encrypt.md5(agentSubInfoDto.getAgentPassword()));
        agentSubInfoDto.setLoginRight(userSubInfoDto.getUserLevel());
        insertAgentInfo(agentSubInfoDto);
        messageDto = MessageEnumDto.SUCCESS;
        return messageDto;
    }

    /**
     * 插入代理表数据
     * @param agentInfoDto
     * @return
     */
    public int insertAgentInfo(AgentInfoDto agentInfoDto){
        return agentInfoDao.insertAgentInfo(agentInfoDto);
    }

    /**
     * 给下属玩家充值
     * @param userId
     * @param uid
     * @param count
     * @return
     */
	public MessageEnumDto charge(Long userId, Long uid, Integer count) {
		MessageEnumDto messageDto = MessageEnumDto.FAIL;
		
		if(count == null){
			messageDto.setMessageValue("请输入充值金额");
            return messageDto;
    	}
		
		if(userId == null){
			messageDto.setMessageValue("请输入充值来源玩家");
            return messageDto;
    	}
		
		if(uid == null){
			messageDto.setMessageValue("请输入充值目标玩家");
            return messageDto;
    	}

		//验证金币数够不够
    	UserInfoDto src = userInfoService.queryUserInfo(userId,null);
    	if(userId.equals(uid)){
    		messageDto.setMessageValue("不能给自己充值");
            return messageDto;
    	}
    	if(src == null){
    		messageDto.setMessageValue("充值的来源玩家不存在");
            return messageDto;
    	}
    	
    	
    	
    	if(count > (src.getGoldCoinCount() == null?0:src.getGoldCoinCount())){
    		messageDto.setMessageValue("你的金币数不足");
            return messageDto;
    	}
    	
    	
    	
    	UserInfoDto dest = userInfoService.queryUserInfo(uid,null);
    	if(dest == null){
    		messageDto.setMessageValue("要充值的目标玩家不存在");
            return messageDto;
    	}
    	
    	userManagerDao.chargeJB(userId,Long.valueOf(count*-1));
    	userManagerDao.chargeJB(uid,Long.valueOf(count));

        UserChargeGoldCoinLogDto userChargeGoldCoinLogDto = new UserChargeGoldCoinLogDto();
        userChargeGoldCoinLogDto.setJuniorUserId(uid);
        userChargeGoldCoinLogDto.setSuperiorUserId(src.getUserId());
        userChargeGoldCoinLogDto.setSuperiorAgentId(src.getAgentId());
        userChargeGoldCoinLogDto.setChargeCount(Long.valueOf(count));
        userChargeGoldCoinLogDto.setSourceType(Contants.USER_CHARGE_SOURCE_TYPE_WX);
        userChargeGoldCoinLogDto.setCreateTime(LocalDateTime.now());
        userChargeGoldCoinLogDao.insert(userChargeGoldCoinLogDto);

    	messageDto = MessageEnumDto.SUCCESS;
        return messageDto;
	}
}
